package com.fg.cloud.factory.controller.agency;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.AgencyBill;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.agency.AgencyBillEntity;
import com.fg.cloud.manager.entry.agency.vo.AgencyBillVo;
import com.fg.cloud.manager.service.agency.AgencyBillService;

/**
 * Description:厂商确认收款controller
 * 
 * @author shaoqiping
 * @date 2017年8月21日上午11:20:41
 */
@Controller
@RequestMapping("/agencyConfirm")
public class AgencyConfirmController extends BaseController {

	@Autowired
	AgencyBillService billService;

	/**
	 * Description:厂商流水确认
	 * 
	 * @param request
	 * @param response
	 * @param entity
	 * @author shaoqiping
	 * @date 2017年8月24日下午1:53:25
	 */
	@RequestMapping(value = "/confirmAmount", method = { RequestMethod.POST })
	@ResponseBody
	public void modify(HttpServletRequest request, HttpServletResponse response, AgencyBillEntity entity) {
		try {
			if (entity == null || LongUtils.isEmpty(entity.getId())) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			AgencyBillEntity temp = billService.selectToCheck(entity.getId());
			if (temp == null) {
				entity.setTransInfo(null);
			} else {
				if(temp.getAmount().compareTo(entity.getConfirmAmount())<0){
					doResult(response, false, StatusCode.SAVEERROR, null, "确认金额不能高于待确认金额");
					return;
				}
				entity.setTransInfo(temp.getTransInfo());
				entity.setTransMode(temp.getTransMode());
				entity.setOrderId(temp.getOrderId());
				entity.setTransId(temp.getTransId());
			}
			if (entity.getTransInfo() != AgencyBill.AMOUNT_ADD && entity.getTransInfo() != AgencyBill.ORDERPAY
					&& entity.getTransInfo() != AgencyBill.REPAYMENT) {
				doResult(response, false, StatusCode.SAVEERROR, null, "请确认数据的有效性");
				return;
			}
			entity.setCollectionAccount(temp.getCollectionAccount());
			entity.setFacId(getCurrentFactoryUser().getFacId());
			entity.setConfirmTime(new Date());
			entity.setStatus(1);
			entity.setAdmin(getCurrentFactoryUser().getTrueName());
			boolean flag = billService.confirm(entity);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:经销商账户未确认流水
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param filterText
	 * @param startTime
	 * @param endTime
	 * @param entity
	 * @author shaoqiping
	 * @date 2017年8月21日下午2:13:42
	 */
	@RequestMapping(value = "/findUnconfirmBills", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void findUnconfirmBills(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,
			String sort, String order, String filterText, String startTime, String endTime, AgencyBillVo entity) {
		try {
			// page默认为1 rows默认为10
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<AgencyBillVo> pageinfo = new PageUtils<AgencyBillVo>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("filterText", filterText);
			condition.put("facId", getCurrentFactoryUser().getFacId());
			condition.put("ageId", entity.getAgeId());
			condition.put("collectionAccount", entity.getCollectionAccount());
			condition.put("startTime", startTime);
			condition.put("endTime", endTime);
			condition.put("status", AgencyBill.UN_STATUS);
			condition.put("transInfo", entity.getTransInfo());
			condition.put("flag", AgencyBill.FAC);
			condition.put("flag4", AgencyBill.FLAG);
			condition.put("valueAdd", AgencyBill.VALUE_ADD);
			pageinfo.setCondition(condition);

			billService.findList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
